package com.yuntsg.nnsfcp.controller.back;


import com.yuntsg.nnsfcp.common.beans.ResultBean;
import com.yuntsg.nnsfcp.common.utils.CheckUtil;
import com.yuntsg.nnsfcp.entity.PermissionRespNode;
import com.yuntsg.nnsfcp.front.entity.TPermission;
import com.yuntsg.nnsfcp.front.mapper.TPermissionMapper;
import com.yuntsg.nnsfcp.service.CommonService;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/sys")
@RestController
//@Api(tags = "组织模块-菜单权限管理")
public class PermissionController {

    //2021年1月22日10:41:43 修改权限和菜单 参照的童工的代码

    @Resource
    CommonService commonService;
    @Resource
    private TPermissionMapper tPermissionMapper;

    @PostMapping("/add-permission")
    //@ApiOperation(value = "新增菜单权限接口")
    public ResultBean<Object> addPermission(@RequestBody TPermission vo) {
        TPermission tPermission = commonService.addPermission(vo);
        return new ResultBean<>(tPermission);
    }

    @GetMapping("/del-permission/{id}")
    //@ApiOperation(value = "删除菜单权限接口")
//    @ApiImplicitParam(value = "id", name = "id", required = true, dataType = "String")
    public ResultBean<Object> deleted(@PathVariable("id") Integer id) {
        commonService.deletedPermisson(id);
        return new ResultBean<>("");
    }

    @PostMapping("/permission")
    //@ApiOperation(value = "更新菜单权限接口")
    public ResultBean<Object> updatePermission(@RequestBody TPermission vo) {
        if (StringUtils.isEmpty(vo.getId())) {
            CheckUtil.check(false, "id不能为空");
        }
        commonService.updatePermission(vo);
        return new ResultBean<>();
    }

    @GetMapping("/permission/{id}")
    //@ApiOperation(value = "查询菜单权限接口")
//    @ApiImplicitParam(value = "id", name = "id", required = true, dataType = "String")
    public ResultBean<Object> detailInfo(@PathVariable("id") Integer id) {
        if (StringUtils.isEmpty(id)) {
            CheckUtil.check(false, "id不能为空");
        }
        return new ResultBean<>(tPermissionMapper.selectById(id));
    }

    @GetMapping("/permissions")
    //@ApiOperation(value = "获取所有菜单权限接口")
    public ResultBean<Object> getAllMenusPermission() {
        List<TPermission> tPermissions = commonService.selectAll();
        return new ResultBean<>(tPermissions);
    }

    @GetMapping("/permission/tree")
    //@ApiOperation(value = "获取所有目录菜单树接口")
//    @ApiImplicitParam(value = "permissionId", name = "permissionId", required = false, dataType = "String")
    public ResultBean<Object> getAllMenusPermissionTree(Integer permissionId) {
        List<PermissionRespNode> permissionRespNodeList = commonService.selectAllMenuByTree(permissionId);
        return new ResultBean<>(permissionRespNodeList);
    }

    @GetMapping("/permission/tree/all")
    //@ApiOperation(value = "获取所有目录菜单树接口")
    public ResultBean<List<PermissionRespNode>> getAllPermissionTree() {
        return new ResultBean<>(commonService.selectAllByTree());
    }


//    @GetMapping("/roles")
//    //@ApiOperation(value = "获取所有角色菜单树接口")
//    public ResultBean<Object> getAllRoles(@RequestParam(required = false) Integer permissionId) {
//        List<PermissionRespNode> permissionRespNodeList = commonService.selectAllMenuByTree(permissionId);
//        return new ResultBean<>(permissionRespNodeList);
//    }


}
